package com.itmk.user.dao;

import com.itmk.common.BaseDao;
import com.itmk.user.entity.SysUser;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class SysUserDaoImpl extends BaseDao implements SysUserDao{
    @Override
    public SysUser login(String username, String password) {
        //1、定义sql
        String sql = "select * from sys_user where username=? and password=?";
        //2、定义handler
        BeanHandler<SysUser> handler = new BeanHandler<>(SysUser.class,processor);
        //3、定义参数
        Object[] parms = {
                username,
                password
        };
        try {
            return query.query(sql,handler,parms);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public SysUser getUserByPas(int userId, String password) {
        String sql = "select * from sys_user where user_id =? and password=?";
        BeanHandler<SysUser> handler = new BeanHandler<>(SysUser.class,processor);
        Object[] parms = {
                userId,
                password
        };
        try {
            return query.query(sql,handler,parms);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public void updatePassword(int userId, String password) {
        String sql = "update sys_user set password=? where user_id =?";
        Object[] parms = {
              password,
              userId
        };
        try {
            query.update(sql,parms);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
